package com.chookss.video.recommend;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aliyun.player.alivcplayerexpand.view.control.ControlView;
import com.aliyun.player.alivcplayerexpand.widget.AliyunVodPlayerView;
import com.aliyun.player.alivcplayerexpand.widget.AliyunVodPlayerViews;
import com.aliyun.player.source.VidAuth;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.mine.personal.PersonalHomeActivity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.video.entity.VideoEntity;
import com.chookss.view.CircleImageView;
import com.chookss.wxapi.ShareActivity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class RecommendedAdapter extends BaseQuickAdapter<VideoEntity, BaseViewHolderEx> {
    private Context mContext;
    private List<VideoEntity> data;
    private int index=-1;
    public RecommendedAdapter(int layoutResId, Context context, @Nullable List<VideoEntity> data) {
        super(layoutResId, data);
        this.mContext = context;
        this.data = data;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, VideoEntity item) {
        CircleImageView ivHead = helper.getView(R.id.ivHead);
        RelativeLayout rlTop = helper.getView(R.id.rlTop);
        AliyunVodPlayerViews video_view = helper.getView(R.id.video_view);
        TextView tvTime = helper.getView(R.id.tvTime);
        LinearLayout llFocus = helper.getView(R.id.llFocus);
        ImageView ivFocus = helper.getView(R.id.ivFocus);
        TextView tvNumber = helper.getView(R.id.tvNumber);
        TextView tvTiltle = helper.getView(R.id.tvTiltle);
        TextView tvName = helper.getView(R.id.tvName);
        LinearLayout llShare = helper.getView(R.id.llShare);
        LinearLayout llMore = helper.getView(R.id.llMore);
        TextView tvShare = helper.getView(R.id.tvShare);
        TextView tvPl = helper.getView(R.id.tvPl);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rlTop.getLayoutParams();
        params.height = Utils.getScreenWidthPixel(mContext) / 2;
        rlTop.setLayoutParams(params);
        tvTime.setText(Utils.changeTime(item.videoSeconds));
        tvNumber.setText(Utils.changeCounts(item.videoPlayCounts) + "次播放");
        GlideUtils.loadAvatar(mContext, item.headPhotoThumbUrl, ivHead);
        tvName.setText(item.employeeName);
        if (Utils.isNull(item.courseCode)) {
            tvTiltle.setText(item.videoTitle);
        } else {
            tvTiltle.setText(item.courseName);
        }
        tvShare.setText(Utils.changeCounts(item.videoShareCounts));
        tvPl.setText(Utils.changeCounts(item.videoCommentCounts));
        if ("1".equals(item.youIsfans)) {
            ivFocus.setBackgroundResource(R.drawable.iv_focus_24);
        } else {
            ivFocus.setBackgroundResource(R.drawable.iv_add_24);
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, VedioDetailActivity.class);
                intent.putExtra("video", item);
                mContext.startActivity(intent);
            }
        });
        llMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, VedioDetailActivity.class);
                intent.putExtra("video", item);
                mContext.startActivity(intent);
            }
        });
        ivHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                if (Utils.isNull(item.courseCode)) {
                    Intent intent = new Intent(mContext, PersonalHomeActivity.class);
                    intent.putExtra("employeeCode", item.employeeCode);
                    mContext.startActivity(intent);
                }

            }
        });
        llFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                if ("1".equals(item.youIsfans)) {
                    toFollow(item.employeeCode, "0");
                } else {
                    toFollow(item.employeeCode, "1");
                }


            }
        });
        llShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent = new Intent( mContext, ShareActivity.class);
                intent.putExtra("title", item.videoTitle);
                intent.putExtra("createEmployeeName", item.employeeName);
                intent.putExtra("content", item.videoKeyWord);
                intent.putExtra("resourceId", item.videoId);
                intent.putExtra("videoShareCounts", item.videoShareCounts);
                intent.putExtra("time", item.videoSeconds);
                intent.putExtra("picUrl", item.videoCoverPath);
                intent.putExtra("resourceType", 2);
                mContext.startActivity(intent);
            }
        });
        if(index!=helper.getItemPosition()){
            if(video_view!=null){
                video_view.onStop();
            }
        }
        video_view.setCoverUri(item.videoCoverPath);

    }
    /**
     * 请求sts
     */
    private void requestVidSts(AliyunVodPlayerViews video_view,String videoId,int index) {
        Map<String, String> map = new HashMap<>();
        map.put("videoId",videoId);
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.getVideoPlayAuth, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
                JSONObject reObject = new JSONObject(data);
                if ("200".equals(reObject.getString("code"))) {
                    JSONObject jsonObject = reObject.getJSONObject("data");
                    VidAuth vidAuth = new VidAuth();
                    vidAuth.setVid(videoId);
                    vidAuth.setPlayAuth(jsonObject.getString("playAuth"));
                    if (video_view != null) {
                        video_view.setAuthInfo(vidAuth);
                    }
                } else {
                    MyToast.show(reObject.getString("msg"));
                }
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    private void toFollow(String employeeCode, String type) {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("employeeCode", employeeCode);
        allmap.put("followStatus", type);
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.toFollow, SignUtils2.makeSignStr(allmap), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
                JSONObject reObject = new JSONObject(data);
                if ("200".equals(reObject.getString("code"))) {
                    if ("1".equals(type)) {
                        MyToast.show("关注成功");
                        refreshData(employeeCode, "1");
                    } else {
                        MyToast.show("取消关注");
                        refreshData(employeeCode, "0");
                    }

                } else {
                    MyToast.show(reObject.getString("msg"));
                }

            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    private void refreshData(String employeeCode, String type) {
        for (int i = 0; i < data.size(); i++) {
            if (employeeCode.equals(data.get(i).employeeCode)) {
                data.get(i).youIsfans = type;
                notifyItemChanged(i);
            }
        }

    }
}
